<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->
    
        <meta name="description" content="JavaScript Object Notation，另一种轻量级的文本数据交换格式，独立于语言,每种语言都认识它,本质还是字符串。
1、数据在名称/值对中2、数据由逗号分隔(最后一个健/值对不能带逗号)3、花括号保存对象方括号保存数组4、使用双引号12345[  &amp;#123;&amp;quot;src&amp;q">
    

    <!--Author-->
    
        <meta name="author" content="John Doe">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="JSON"/>
    

    <!--Open Graph Description-->
    

    <!--Open Graph Site Name-->
    <meta property="og:site_name" content="Hexo"/>

    <!--Type page-->
    
        <meta property="og:type" content="article" />
    

    <!--Page Cover-->
    

        <meta name="twitter:card" content="summary" />
    

    <!-- Title -->
    
    <title>JSON - Hexo</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet" />

    <!-- Google Analytics -->
    


    <!-- favicon -->
    
	
</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">逐风</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>JSON</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                        
                            2017-02-20
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>JavaScript Object Notation，另一种轻量级的文本数据交换格式，独立于语言,每种语言都认识它,本质还是字符串。</p>
<p>1、数据在名称/值对中<br>2、数据由逗号分隔(最后一个健/值对不能带逗号)<br>3、花括号保存对象方括号保存数组<br>4、使用双引号<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[</div><div class="line">  &#123;&quot;src&quot;:&quot;images/detail01.jpg&quot;,&quot;oldPrice&quot;:&quot;10.12&quot;,&quot;newPrice&quot;:&quot;130.00&quot;&#125;,</div><div class="line">  &#123;&quot;src&quot;:&quot;images/detail02.jpg&quot;,&quot;oldPrice&quot;:&quot;1.00&quot;,&quot;newPrice&quot;:&quot;11.00&quot;&#125;,</div><div class="line">  &#123;&quot;src&quot;:&quot;images/detail03.jpg&quot;,&quot;oldPrice&quot;:&quot;100.00&quot;,&quot;newPrice&quot;:&quot;1000.00&quot;&#125;</div><div class="line">]</div></pre></td></tr></table></figure></p>
<p>JSON数据在不同语言进行传输时，类型为字符串，不同的语言各自也都对应有解析方法，需要解析完成后才能读取</p>
<p>1、PHP解析方法<br>json_encode()、json_decode()<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">    header(&apos;Content-Type:text/html;charset=utf-8&apos;);</div><div class="line">    /*以json格式传输数据的时候要求响应内容格式是   application/json*/</div><div class="line">    /*注意也可以不设置  但是这遵循的一个规范*/</div><div class="line"></div><div class="line">    /*file_get_contents 获取文件内容*/</div><div class="line">    $json = file_get_contents(&apos;01.json&apos;);</div><div class="line"></div><div class="line">    /*输出json内容*/</div><div class="line">    echo $json;</div><div class="line">    echo &apos;&lt;br&gt;&lt;br&gt;&apos;;</div><div class="line"></div><div class="line">    $array = array(</div><div class="line">        array(&apos;src&apos;=&gt;&apos;images/detail01.jpg&apos;,&apos;newPrice&apos;=&gt;&apos;12.00&apos;,&apos;oldPrice&apos;=&gt;&apos;455.00&apos;),</div><div class="line">        array(&apos;src&apos;=&gt;&apos;images/detail02.jpg&apos;,&apos;newPrice&apos;=&gt;&apos;65.00&apos;,&apos;oldPrice&apos;=&gt;&apos;878.00&apos;),</div><div class="line">        array( &apos;src&apos;=&gt;&apos;images/detail01.jpg&apos;,&apos;newPrice&apos;=&gt;&apos;100.00&apos;,&apos;oldPrice&apos;=&gt;&apos;1000.00&apos;)</div><div class="line">    );</div><div class="line"></div><div class="line">    /*将php数组转化成json字符*/</div><div class="line">    $json_array = json_encode($array);</div><div class="line">    echo $json_array;</div><div class="line">    echo &apos;&lt;br&gt;&lt;br&gt;&apos;;</div><div class="line"></div><div class="line">    /*将json字符转化成php数组*/</div><div class="line">    $array_json = json_decode($json_array);</div><div class="line">    echo $array_json;</div><div class="line">    echo &apos;&lt;br&gt;&lt;br&gt;&apos;;</div><div class="line">?&gt;</div></pre></td></tr></table></figure></p>
<p>1、Javascript 解析方法<br>JSON对象 JSON.parse()、JSON.stringify()；<br>JSON兼容处理json2.js<br>总结：JSON体积小、解析方便且高效，在实际开发成为首选。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">var xhr = new XMLHttpRequest;</div><div class="line">xhr.open(&apos;get&apos;,&apos;01.php&apos;);</div><div class="line">xhr.send(null);</div><div class="line">xhr.onreadystatechange = function()&#123;</div><div class="line">    if(xhr.status == 200 &amp;&amp; xhr.readyState == 4)&#123;</div><div class="line">        /*获取仅仅是字符串*/</div><div class="line">        var text = xhr.responseText;</div><div class="line"></div><div class="line">        /*需要把字符串转化成JSON对象*/</div><div class="line">        var json_obj = JSON.parse(text);</div><div class="line">        console.log(json_obj);</div><div class="line"></div><div class="line">        /*我们也可以把JSON对象转化成字符串*/</div><div class="line">        var json_str = JSON.stringify(json_obj);</div><div class="line">        console.log(json_str);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="兼容性"><a href="#兼容性" class="headerlink" title="兼容性"></a>兼容性</h3><p>关于IE的兼容方面 IE5.5 5<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">function XHR() &#123;</div><div class="line">    var xhr;</div><div class="line">    try &#123;</div><div class="line">        xhr = new XMLHttpRequest();</div><div class="line">    &#125;</div><div class="line">    /*如果 try内的程序运行错误  抛出异常  捕捉异常  上面程序当中运行的错误*/</div><div class="line">    catch(e) &#123;</div><div class="line">        /*在不同的IE版本下初始  ActiveXObject  需要传入的标识*/</div><div class="line">        var IEXHRVers =[&quot;Msxml3.XMLHTTP&quot;,&quot;Msxml2.XMLHTTP&quot;,&quot;Microsoft.XMLHTTP&quot;];</div><div class="line"></div><div class="line">        for (var i=0;i&lt;IEXHRVers.length;i++) &#123;</div><div class="line">            try &#123;</div><div class="line">                xhr = new ActiveXObject(IEXHRVers[i]);</div><div class="line">            &#125;</div><div class="line">            catch(e) &#123;</div><div class="line">                /*如果出现错误的时候  停止当次的循环*/</div><div class="line">                continue;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    return xhr;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr />

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2018 John Doe<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>